package com.zql.LeetCode;

/**
 * 304. 二维区域和检索 - 矩阵不可变
 * Created By ShuHua on 2021/8/21.
 */
public class NumMatrix_304 {
    private int[][] nums;

    public void NumMatrix_304_error(int[][] matrix) {
        int n = matrix.length;
        int[][] arr = new int[n][n+1];
        for (int i=0;i< n;i++){

            for(int j=0;j<n+1;j++){
                arr[i+1][j+1] = arr[i][j]+matrix[i][j];
            }
        }
        this.nums=arr;
    }
    public NumMatrix_304(int[][] matrix) {
        int n = matrix.length;
        //这里不是方阵，行列数可以不一致
        int[][] arr = new int[n][matrix[0].length+1];
        for (int i=0;i< n;i++){

            for(int j=0;j<matrix[i].length+1;j++){
                arr[i+1][j+1] = arr[i][j]+matrix[i][j];
            }
        }
        this.nums=arr;
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        int sum=0;
        for(int i=row1;i<=row2;i++){
            sum+=nums[i][col2+1]-nums[i][col1];
        }
        return sum;
    }
}
